package com.duguo.dynamicmvc.view.redirect;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.duguo.dynamicmvc.model.Model;

public class RedirectToRefererView extends RedirectToRootView {
	
	private Pattern _redirectFilter=Pattern.compile(".*(/do[A-Z]).*");
	
    public void render(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {    	
    	String targetUrl = getReferer(request);
    	if(targetUrl!=null && !_redirectFilter.matcher(targetUrl).find())
    		senRedirect(request, response, model,targetUrl);
    	else{
    		super.render(request, response, model);
    	}
    }

	public static String getReferer(HttpServletRequest request) {
		return request.getHeader("Referer");
	}

	public String getRedirectFilter() {
		return _redirectFilter.pattern();
	}

	public void setRedirectFilter(String redirectFilter) {
		_redirectFilter = Pattern.compile(redirectFilter);
	}
}
